var mySwiper;
var app = new Vue({
    el: '#app',
    data: {
        pageFlag: true,
        OrdersBtn: true,
        OrdersBtnPage: false,
        userId: 0,
        id: 0,
        price: 0,
        original_price: 0,
        specsPrice: 0,//商品单价
        specId: 0,
        specName: '',
        head_img: '',
        bannerData: [],
        detailData: [],
        goods_name: '',
        goods_title: '',
        free_shipping: 0,//满减运费限额
        delivery_time: 0,
        freight_money: 0,//运费
        refund_type: '',
        selled_stock: 0,
        total_stock: 0,
        specList: [],
        num: 1,
    },
    created() {
        if ($.getPara('id') != "") {
            this.id = parseInt($.getPara('id'));
            this.getData()
        } else {
            this.pageFlag = false;
            $.toast("异常：获取商品ID失败", "text")
        }
        if ($.getPara("userId") != "") {
            this.userId = parseInt($.getPara("userId"))
        } else {
            if ($.getStorage("userId") != null) {
                this.userId = parseInt($.getStorage("userId"))
            }
        }
    },
    filters: {
        refundTxt(value) {
            var text = "可退可换";
            switch (value) {
            case 2:
                text = "可退不换";
                break;
            case 3:
                text = "不可退可换";
                break;
            case 4:
                text = "不退不换";
                break
            }
            return text
        }
    },
    mounted() {
        this.$nextTick(function() {
            mySwiper = new Swiper('.swiper-container',{
                autoplay: 3000,
                loop: true,
                pagination: '.swiper-pagination',
                autoplayDisableOnInteraction: false,
                observer: true,
                observeParents: false,
                onSlideChangeEnd: function(swiper) {
                    swiper.update();
                    mySwiper.startAutoplay();
                    mySwiper.reLoop()
                }
            })
        })
    },
    methods: {
        getData() {
            var _this = this;
            $.ajax({
                type: "GET",
                url: '/api/goods/detail',
                data: {
                    id: _this.id,
                },
                success: function(res) {
                    if (res.code == 200) {
                        var data = res.data;
                        _this.delivery_time = data.delivery_time;
                        _this.free_shipping = data.free_shipping;
                        _this.freight_money = data.freight_money;
                        _this.goods_name = data.goods_name;
                        _this.goods_title = data.goods_title;
                        _this.refund_type = data.refund_type;
                        _this.delivery_time = data.delivery_time;
                        _this.selled_stock = data.selled_stock;
                        _this.state = data.state;
                        _this.total_stock = data.total_stock;
                        _this.head_img = data.head_img;
                        _this.bannerData = JSON.parse(data.banner_img);
                        _this.detailData = JSON.parse(data.goods_detail);
                        _this.price = data.specList[0].price;
                        _this.specsPrice = data.specList[0].price;
                        _this.specId = data.specList[0].spec_id;
                        _this.specName = data.specList[0].spec;
                        _this.original_price = data.specList[0].original_price;
                        _this.specList = data.specList;
                        if (_this.selled_stock >= _this.total_stock) {
                            _this.OrdersBtn = false
                        }
                    } else {
                        $.toast(res.msg, "text")
                    }
                },
                error: function() {
                    $.toast("加载失败，请重试", "text")
                }
            })
        },
        goodsClose() {
            $("body").removeClass("van-overflow-hidden");
            $(".van-sku-container").hide();
            $(".van-modal").hide()
        },
        subtractNum() {
            if (this.num <= 1) {
                return false
            } else {
                this.num--
            }
        },
        addNum() {
            var num = subtractNumber(this.total_stock, this.selled_stock);
            if (this.num > num) {
                $.toptip('商品只剩' + num + '余件啦~', 'error')
            } else {
                this.num++
            }
        },
        selctSpec(item) {
            this.specId = item.spec_id;
            this.specsPrice = item.price;
            this.specName = item.spec
        },
        Orders() {
            if (!this.OrdersBtn) {
                $.toptip('商品已卖光啦~', 'error')
            } else {
                $("body").addClass("van-overflow-hidden");
                $(".van-sku-container").show();
                $(".van-modal").show()
            }
        },
        submitBtn() {
            if ($.getPara("userId") != "" && this.userId > 0) {
                $.showLoading("请求中...");
                var _this = this;
                var obj = {};
                obj.id = this.id;
                obj.head_img = this.head_img;
                obj.goods_title = this.goods_title;
                obj.specId = this.specId;
                obj.specName = this.specName;
                obj.specsPrice = this.specsPrice;
                obj.num = this.num;
                obj.freight_money = this.freight_money;
                // debugger;
                //判断是否满足免运费
                // if (subtractNumber(SubNumber(this.num, this.specsPrice), this.free_shippiing) >= 0) {
                //     obj.isFree = true
                // } else {
                //     obj.isFree = false
                // }
                let totalPrice = this.num * this.specsPrice;//订单总共金额
                // console.log(totalPrice,this.free_shipping);
                if(totalPrice >= _this.free_shipping){//达到满减要求
                    obj.isFree = true
                }else{
                    obj.isFree = false
                }
                if (obj.isFree) {
                    obj.amount = SubNumber(this.num, this.specsPrice).toFixed(2)
                } else {
                    obj.amount = andNumber(SubNumber(this.num, this.specsPrice), this.freight_money).toFixed(2)
                }
                if ($.getStorage("goods") && $.getStorage("goods") != null) {
                    $.delStorage("goods")
                }
                $.setStorage("goods", obj, true);
                _this.goodsClose();
                setTimeout(function() {
                    var para = {};
                    para.title = "立即结算";
                    para.type = "shop";
                    para.url = '/PageClassify/html/Seckill/seckillSettle.html?id=' + _this.id;
                    $.hideLoading();
                    if (this.userId > 0) {
                        para.url = '/PageClassify/html/Seckill/seckillSettle.html?id=' + _this.id + "&userId=" + _this.userId
                    }
                    if (/(Android)/i.test(navigator.userAgent)) {
                        jsAndroid.appPush(JSON.stringify(para))
                    } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                        window.webkit.messageHandlers.appPush.postMessage(para)
                    } else {
                        window.location.href = para.url;
                        alert("PC浏览器")
                    }
                }, 1200)
            } else {
                if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                    window.webkit.messageHandlers.appLogin.postMessage(null)
                } else {
                    alert("PC浏览器-appLogin")
                }
            }
        }
    }
})
